﻿using Firestarter.DB.Schema.BO;
using FireStarterModeller.BO;
using Habanero.Base.Exceptions;

namespace Firestarter.DB_DomainModel.Logic
{
    public class DBToDMReverseEngineeringController
    {
        protected IPropertyTypeStorer PropertyTypeStorer;

        public DBToDMReverseEngineeringController(DMSolution solution)
        {
            if (solution==null) throw new HabaneroDeveloperException("Solution is null");
            Solution = solution;
        }

        public DMSolution Solution { get; private set; }

        public IDBToDMImporterFactory CreateDBToDMImporterFactory()
        {
            PropertyTypeStorer = new PropertyTypeStorer(Solution);
            return new DBToDMImporterFactory(new DBTypeToDMPropertyTypeMapper(PropertyTypeStorer), new RelationshipNameGenerator());
        }

        public ReverseEngineering CreateReverseEngineeringForSolution(ReverseEngineeringSettings reverseEngineeringSettings, DatabaseConnectionInfo databaseConnectionInfo)
        {
            if (reverseEngineeringSettings == null) throw new HabaneroDeveloperException("reverseEngineeringSettings is null");
            if (databaseConnectionInfo == null) throw new HabaneroDeveloperException("databaseConnectionInfo is null");

            if (Solution.ReverseEngineering == null)
            {
                var reverseEngineering = new ReverseEngineering
                {
                    ReverseEngineeringSettings = reverseEngineeringSettings,
                    DatabaseConnectionInfo = databaseConnectionInfo
                };
                Solution.ReverseEngineering = reverseEngineering;
}

            return Solution.ReverseEngineering;
        }

    }


}
